//
//  WSUpdateFilterConditionDropdownView.m
//  microvision
//
//  Created by zhenjingxiong on 2021/3/17.
//  Copyright © 2021 tencent. All rights reserved.
//

#import "WSUpdateFilterConditionDropdownView.h"
#import "WSDataReportFilterItem.h"
#import "WSDataReportHelper.h"

static NSString * const gIdentifier  = @"cell";

@interface WSUpdateFilterConditionDropdownView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *dropdownListView;

@property (nonatomic, copy) NSArray<NSString *> *dropdownDataSource;

@end

@implementation WSUpdateFilterConditionDropdownView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI {
    self.dropdownListView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.dropdownListView];
    [self addConstraints:[self dropdownListViewConstraints]];

    [self.dropdownListView registerClass:[UITableViewCell class] forCellReuseIdentifier:gIdentifier];
}


#pragma mark - TablelView Method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dropdownDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:gIdentifier forIndexPath:indexPath];
    cell.textLabel.text = self.dropdownDataSource[indexPath.row];
    cell.backgroundColor = [UIColor redColor];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.font = [UIFont systemFontOfSize:kReportDatafilterFontOfSize];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(didSelectDropdownContent:)]) {
        NSString *dropdownContent = self.dropdownDataSource[indexPath.row];
        [self.delegate didSelectDropdownContent:dropdownContent];
    }
}

#pragma mark - Public Method

- (void)updateDropdownDataSource:(NSArray *)dropdownDataSource {
    self.dropdownDataSource = dropdownDataSource;
    [self.dropdownListView reloadData];
}

#pragma mark - Accessor Method

- (UITableView *)dropdownListView {
    if (!_dropdownListView) {
        _dropdownListView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _dropdownListView.dataSource = self;
        _dropdownListView.delegate = self;
        _dropdownListView.rowHeight = 44;
    }
    return _dropdownListView;
}

#pragma mark - Automatic layout Method

- (NSArray<NSLayoutConstraint *> *)dropdownListViewConstraints {
    NSLayoutConstraint *dropdownListViewTop = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropdownListView toItem:self attribute:NSLayoutAttributeTop];
    NSLayoutConstraint *dropdownListViewBottom = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropdownListView toItem:self attribute:NSLayoutAttributeBottom];
    NSLayoutConstraint *dropdownListViewLeft = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropdownListView toItem:self attribute:NSLayoutAttributeLeading];
    NSLayoutConstraint *dropdownListViewTrailing = [WSDataReportHelper equallyRelatedConstraintWithView:self.dropdownListView toItem:self attribute:NSLayoutAttributeTrailing];
    NSArray *constraints = @[dropdownListViewTop, dropdownListViewBottom, dropdownListViewLeft, dropdownListViewTrailing];
    return constraints;
}

@end
